package com.hmkj.web.controller.find;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.core.model.user.UserModel;
import com.hmkj.core.po.amount.AccountLog;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.amount.AccountLogService;
import com.hmkj.core.service.order.OrderService;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "推广赚钱相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/find")
public class PopularizeController extends GenericController<Integer, User> {

    @Resource
    private AccountLogService accountLogService;
    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;

    @ApiOperation(value = "推广赚钱", notes = "推广赚钱")
    @ResponseBody
    @RequestMapping(value = "popularize", method = RequestMethod.POST)
    public ResultEntity popularize() {
        User user = getLoginUser();
        //已获取佣金，
        BigDecimal reward = accountLogService.selectSumByAirectionType(AccountLog.DIRECTION.RECEIVE, AccountLog.TYPE.T11, user.getId());
        if (reward == null) {
            reward = BigDecimal.ZERO;
        }
        // 佣金比例
        User u = userService.selectByPrimaryKey(user.getId());
        if (u.getRewardScale() == null) {
            u.setRewardScale(BigDecimal.ZERO);
        }
        // 我的下线用户
        List<User> userList = userService.select(new User(user1 -> {
            user1.setParentId(u.getId());
            user1.setDelFlag(User.DELFLAG.NORMAL.code);
        }));
        int inviteNum = 0;
        int tradeNum = 0;
        if (CollectionUtil.isNotEmpty(userList)) {
            inviteNum = userList.size();
            for (User us : userList) {
                List<Order> orders = orderService.selectByStatusUserId(us.getId(), Order.STATUS.T3.code, Order.STATUS.T4.code, Order.STATUS.T5.code, Order.STATUS.T6.code);
                if (CollectionUtil.isNotEmpty(orders)) {
                    tradeNum = tradeNum + 1;
                }
            }
        }
        //推广链接
        String popularizeUrl = "http://" + u.getSource() + "/register1?inviteCode=" + u.getInviteCode();
        Map<String, Object> map = new HashMap<>();

        map.put("reward", reward);
        map.put("rewardScale", u.getRewardScale());
        map.put("popularizeUrl", popularizeUrl);
        map.put("inviteNum", inviteNum);
        map.put("tradeNum", tradeNum);
        return ok(map);
    }

    @ApiOperation(value = "我的下线", notes = "我的下线")
    @ResponseBody
    @RequestMapping(value = "inviteList", method = RequestMethod.POST)
    public ResultEntity inviteList() {
        User user = getLoginUser();
        List<User> userList = userService.selectOffline(user.getId());
        List<UserModel> users = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userList)) {
            for (User u : userList) {
                int tradeNum = 0;
                UserModel model = new UserModel();
                List<Order> orders = orderService.selectByStatusUserId(u.getId(), Order.STATUS.T3.code, Order.STATUS.T4.code, Order.STATUS.T5.code, Order.STATUS.T6.code);
                if (CollectionUtil.isNotEmpty(orders)) {
                    for (Order o : orders) {
                        tradeNum = tradeNum + o.getNumber();
                    }
                }
                model.setTradeNum(tradeNum);
                model.setUserName(u.getUserName());
                model.setCreateTime(u.getCreateTime());
                users.add(model);
            }
        }
        return ok(users);
    }


}
